import React from "react";
import { FormInstance, FormProps, Store } from "./interface";
import useForm from "./useForm";

import FormContext from "./FormContext";

const Form: React.ForwardRefRenderFunction<FormInstance, any> = (props) => {
  const {
    initialValues,
    name,
    form,
    onFinish,
    children,
    // component: Component = "form",
  } = props;
  // const formContext = React.useContext(FormContext);
  const [formInstance] = useForm(form);
  const { setCallbacks } = formInstance;

  setCallbacks({
    onFinish: (values: Store) => {
      if (onFinish) {
        onFinish(values);
      }
    },
  });
  // 设置初始值
  // setInitialValues(initialValues, !mountRef.current);

  return (
    <form
      onSubmit={(event: React.FormEvent<HTMLFormElement>) => {
        event.preventDefault();
        event.stopPropagation();
        // onFinish
        formInstance.submit();
      }}
    >
      {/* @ts-ignore */}
      <FormContext.Provider value={formInstance}>
        {children}
      </FormContext.Provider>
    </form>
  );
};

export default Form;
